package org.anti996.lms.controller;

import io.swagger.annotations.ApiOperation;
import org.anti996.lms.model.*;
import org.anti996.lms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/lms/input")
@CrossOrigin(origins = "*")
public class BookinginputController {
    @Autowired
    private BookingService bookingService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private TransportService transportService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private TruckService truckService;
    @Autowired
    private OperatorService operatorService;

    private int calculateMoney(int weight) {
        return weight <= 1 ? 12 : ((weight - 1) * 8 + 12);
    }

    @GetMapping("/money")
    public int getMoney(@RequestParam("weight") double weight) {
        return calculateMoney((int) weight);
    }

    @ApiOperation(value = "货单的增加")
    // @PostMapping(path="/add",consumes = "application/json")
    @PostMapping(consumes = "application/json")  // post已经有动词的含义了，就不要再加add了
    public boolean insertBooking(@RequestBody Bookinginput bookinginput) {
        Booking booking = new Booking();
        Transport transport = new Transport();
        Driver driver = new Driver();
        if (customerService.isexist(bookinginput.getCfname(), bookinginput.getCfphone(), bookinginput.getCfaddr())) {
            booking.setCIdFrom(customerService.getIdbynps(bookinginput.getCfname(), bookinginput.getCfphone(), bookinginput.getCfaddr()).getCId());
        } else {
            Customer customer = new Customer();
            customer.setName(bookinginput.getCfname());
            customer.setPhone(bookinginput.getCfphone());
            customer.setAddr(bookinginput.getCfaddr());
            customerService.save(customer);
            booking.setCIdFrom(customerService.getIdbynps(bookinginput.getCfname(), bookinginput.getCfphone(), bookinginput.getCfaddr()).getCId());
        }
        if (customerService.isexist(bookinginput.getCtname(), bookinginput.getCtphone(), bookinginput.getCtaddr())) {
            booking.setCIdTo(customerService.getIdbynps(bookinginput.getCtname(), bookinginput.getCtphone(), bookinginput.getCtaddr()).getCId());
        } else {
            Customer customer = new Customer();
            customer.setName(bookinginput.getCtname());
            customer.setPhone(bookinginput.getCtphone());
            customer.setAddr(bookinginput.getCtaddr());
            customerService.save(customer);
            booking.setCIdTo(customerService.getIdbynps(bookinginput.getCtname(), bookinginput.getCtphone(), bookinginput.getCtaddr()).getCId());
        }
        booking.setOIdFrom(bookinginput.getOfid());
        booking.setPayBefore(bookinginput.getPayBefore());
        booking.setValue(bookinginput.getValue());
        booking.setWeight(bookinginput.getWeight());
        booking.setMoney(
                calculateMoney(((Double) bookinginput.getWeight()).intValue())
        );
        if (bookinginput.getOfid() == null) {
            booking.setWIdFrom(warehouseService.getIdbywname(bookinginput.getWfname()).getWId());
        } else {
            booking.setWIdFrom(operatorService.getById(bookinginput.getOfid()).getWId());
        }
        booking.setWIdTo(warehouseService.getIdbywname(bookinginput.getWtname()).getWId());
        if (transportService.isexist(booking.getWIdFrom(), booking.getWIdTo())) {
            transport = transportService.findByW(booking.getWIdFrom(), booking.getWIdTo());
        } else {
            transport.setWIdFrom(booking.getWIdFrom());
            transport.setWIdTo(booking.getWIdTo());
            if (driverService.isexist(booking.getWIdFrom())) {
                transport.setDId(driverService.getdIdBywId(booking.getWIdFrom()).getDId());
                boolean f = driverService.updateById(driverService.getdIdBywId(booking.getWIdFrom()));
            }
            if (truckService.isexist(booking.getWIdFrom())) {
                transport.setTId(truckService.gettIdBywId(booking.getWIdFrom()).getTId());
                boolean f = truckService.updateById(truckService.gettIdBywId(booking.getWIdFrom()));
            }
            transportService.save(transport);
        }
        booking.setTNum(transport.getTNum());
        return bookingService.save(booking);
    }
}
